package com.xinnet.core.validate;

import java.util.Properties;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.xinnet.core.validate.attribute.CheckCase;



public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {
	/**
	 * 初始方法
	 * */	
    public void initialize(CheckCase constraintAnnotation) {

    }
    private static Properties prop;
	/**
	 * 接口的验证方法
	 * */	
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintContext) {

    	if(valid(value)){
    		return true;
    	}else{
    		constraintContext.disableDefaultConstraintViolation();
    		constraintContext.buildConstraintViolationWithTemplate(getMessage()).addConstraintViolation();
    		return false;
    	}
    }
	/**
	 * 未通过验证时,提示错误信息
	 * */	
	public String getMessage() {
		this.getClass().getCanonicalName();
		return prop.getProperty(this.getClass().getName());
	}
	/**
	 * 验证属性的方法
	 * 未通过验证:返回false;
	 * 通过验证: 返回 true;
	 * */	
    public boolean valid(String value){
    	 if (value == null) {
    	      return false;
    	    } else if (value instanceof String) {
    	      return ((String) value).length() > 0;
    	    } else {
    	      return true;
    	    }
    }
}
